package com.hh.service.Impl;

import com.hh.dto.ResultDTO;
import com.hh.mapper.NewsMapper;
import com.hh.model.NewsModel;
import com.hh.model.ProductModel;
import com.hh.service.NewsService;
import com.hh.until.JSONCopy;
import com.hh.vo.NewsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public ResultDTO add(NewsVO newsVO) {
        NewsModel newsModel = JSONCopy.copy(newsVO, NewsModel.class);
        return ResultDTO.getDto(newsMapper.add(newsModel), null);
    }

    @Override
    public ResultDTO delete(NewsVO newsVO) {
        NewsModel newsModel = JSONCopy.copy(newsVO, NewsModel.class);
        return ResultDTO.getDto(newsMapper.delete(newsModel), null);
    }

    @Override
    public ResultDTO update(NewsVO newsVO) {
        NewsModel newsModel = JSONCopy.copy(newsVO, NewsModel.class);
        return ResultDTO.getDto(newsMapper.update(newsModel), null);
    }

    @Override
    public ResultDTO findAll(NewsVO newsVO) {
        NewsModel newsModel = JSONCopy.copy(newsVO, NewsModel.class);
        newsModel.setPage((newsModel.getPage()-1)*newsModel.getLimit());
        List<NewsModel> newsModelList = newsMapper.findAll(newsModel);
        return ResultDTO.getDto(newsMapper.count(newsModel), newsModelList);
    }

    @Override
    public ResultDTO findById(NewsVO newsVO) {
        NewsModel newsModel = JSONCopy.copy(newsVO, NewsModel.class);
        return ResultDTO.getDto(0, newsMapper.findById(newsModel));
    }

    @Override
    public ResultDTO count(NewsVO newsVO) {
        NewsModel newsModel = JSONCopy.copy(newsVO, NewsModel.class);
        return ResultDTO.getDto(newsMapper.count(newsModel), null);
    }
}
